package binarytree

func searchBST(root *TreeNode, val int) *TreeNode {
	if root == nil {
		return nil
	}

	var res *TreeNode

	if root.Val == val {
		return root
	} else if root.Val < val {
		res = searchBST(root.Right, val)
	} else if root.Val > val {
		res = searchBST(root.Left, val)
	}

	return res
}

func searchBST2(root *TreeNode, val int) *TreeNode {
	for root != nil {
		if root.Val == val {
			return root
		}
		if val < root.Val {
			root = root.Left
		} else {
			root = root.Right
		}
	}
	return nil
}
